hi i am want to when login succesfully then call my success function otherwise call error function
View code here
<div class="container">
<div class="login-container">
<div class="avatar"><img src="@Url.Content("~/Content/images/download.jpeg")" style="max-width:95%;" /></div>
<div class="form-box">
@using (Html.BeginForm())
{
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
@Html.TextBoxFor(m => m.UserId, new { @class = "form-control", @id="userid", @placeholder = "Username", @required = "required", @maxlength = "20" })
</div>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
@Html.PasswordFor(m => m.Password, new { @class = "form-control", @id = "userpass", @placeholder = "Password", @required = "required", @maxlength = "20" })
</div>
<button class="btn btn-info btn-block login" type="submit" id="login-btn"><i class="glyphicon glyphicon-log-in"></i> Login</button>
}
</div>
</div>
</div>
ajax code here:
<script>
$(document).ready(function () {
$('#login-btn').click(function () {
var dataObject = {
Id: $("#userid").val(),
Password: $("#userpass").val()
};
$.ajax({
url: '@Url.Action("Login","Account")',
type: "POST",
data: dataObject,
dataType: "json",
success: function (data) {
if (data.toString() == "login") {
toastr['success']("Login Successfully");
}
else if (data.toString() == "error") {
toastr['error']("Id or Password is incorrect");
}
},
error: function () {
toastr['error']("Hello");
}
});
});
});
</script>
Controller Code here:
[HttpPost]
public ActionResult Login(LoginMaster model)
{
string message = "";
if (ModelState.IsValid)
{
try
{
var user = from emp in db.LoginMasters
where emp.UserId == model.UserId && emp.Password == model.Password
select emp;
var rol = user.FirstOrDefault();
if (rol != null)
{
var realrol = rol.Role;
if (realrol == "admin")
{
message = "login";
return RedirectToAction("Index", "Home");
}
else if (realrol == "user")
{
Session["userid"] = rol.UserId;
message = "login";
return RedirectToAction("User", "Home");
}
}
else
{
message = "error";
}
}
catch (Exception ex)
{
ViewBag.cath = ex.Message;
}
}
else
{
message = "error";
}
if (Request.IsAjaxRequest())
{
return new JsonResult { Data = message, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
return View();
I am want to when we login succesfully that time call this
toastr['success']("Login Successfully");
and when login fail that time call
toastr['error']("Id or Password is incorrect");
please solve this problem.
thanks in advance!
Gaurpriya Bishnoi
07-Jul-2015John Smith
06-Jul-2015http://www.mindstick.com/Articles/f769698f-fed6-43eb-8e61-d7baaf713819/Login%20and%20Registration%20Form%20in%20ASP%20Net%20MVC#.VZqfdfkSyKs
http://www.mindstick.com/Blog/648/Login%20Form%20in%20Asp%20Net%20MVC%204#.VZqfgfkSyKs
http://www.mindstick.com/blog/647/Creating%20a%20simple%20data%20entry%20application%20using%20Asp%20Net%20MVC%204#.VZqfePkSyKs
http://www.mindstick.com/blog/795/Post%20data%20With%20Ajax%20and%20ASP%20NET%20MVC#.VZqfTPkSyKs